HTMLInputElement.prototype.GetOutput = function()
{
	return (this.value != "" && this.value != this.defaultValue) ? this.value : null;
};

HTMLInputElement.prototype.InitPlaceHolder = function()
{
	var element = this;
	element.onfocus = function()
	{
		if(element.value == element.defaultValue)
			element.value = "";
	};

	element.onblur = function()
	{
		if(!element.value || element.value.lenght < 1)
			element.value = element.defaultValue;
	};
};

var RegisterTypeSeleced = 3;
function CompleteResendEmail()
{
	var email = document.getElementById("Email_RegF").value;
	var onfinish = function(responseText)
	{
		var res = JSON.parse(responseText);

		if(res.code == 1)
		{
			document.getElementById("registertab").style.visibility = "visible";
			document.getElementById("registertab").fading(666,0,100);

			thankyoupage();
		}
		else alert("Registering Failed : code : " + res.code + " : " + res.status);
	};
	var onerror = function(statusText)
	{
		alert("CompleteResendEmail response error : " + statusText);
	};

	HttpAsyncRequest("POST","/register?email=" + email,null,onfinish,onerror);

	closeTab();
}

function openTab()
{
	var regEmail = document.getElementById("RegisterID").GetOutput();
	if(regEmail == null || regEmail.indexOf("@",0) < 1)
	{
		document.getElementById("RegisterIDText").style.color = "#FF0000";
		return;
	}
	document.getElementById("RegisterIDText").style.color = "inherit";

	var regPass = document.getElementById("RegisterPass").GetOutput();
	if(regPass == null || regPass.length < 6)
	{
		document.getElementById("RegisterPassText").style.color = "#FF0000";
		return;
	}
	document.getElementById("RegisterPassText").style.color = "inherit";

	if(regPass != document.getElementById("RegisterRepeatPass").GetOutput())
	{
		document.getElementById("RegisterRepeatPassText").style.color = "#FF0000";
		return;
	}
	document.getElementById("RegisterRepeatPassText").style.color = "inherit";

	var onfinish = function(responseText)
	{
		var res = JSON.parse(responseText);

		if(res.code == 1)
		{
			document.getElementById("registertab").style.visibility = "visible";
			document.getElementById("registertab").fading(666,0,100);

			thankyoupage();
		}
		else alert("Registering Failed : code : " + res.code + " : " + res.status);
	};
	var onerror = function(statusText)
	{
		alert("Register response Error : " + statusText);
	};

	HttpAsyncRequest("POST","/register?email=" + regEmail + "&password=" + regPass,null,onfinish,onerror);
}
function closeTab()
{
	document.getElementById("registertab").fading(666,100,0);
}

function Select(key,type)
{
	if(key == null || key.length < 1)
	{
		alert("Why you can access here with this key ( " + key + " )!?, Don't Hack!");
		return;
	}
	if(type != 1 && type != 2 && type != 3)
	{
		alert("Wrong Type!");
		return;
	}

	RegisterTypeSeleced = type;
	document.getElementById("registerform").fading(666,0,100);
	document.getElementById("registerform").rows[0].cells[0].innerHTML = LoadFile("GET","RegisForm" + type + ".htm");

	document.getElementById("regisImg").setAttribute("src","imgs/doctype" + type + ".png");

	document.getElementById("Name_RegF").InitPlaceHolder();
	document.getElementById("ID_RegF").InitPlaceHolder();
	if(type == 1)
		document.getElementById("Other_RegF").InitPlaceHolder();

	var regisButton = document.getElementById("regisButton");
	regisButton.setAttribute("class","bttab" + type);
	regisButton.onclick = function()
	{
		var pid = document.getElementById("ID_RegF");
		var name = document.getElementById("Name_RegF");
		var other = document.getElementById("Other_RegF");
		if(name == null || name.GetOutput() == null)
		{
			alert("Missing Name argument");
			return;
		}
		if(pid == null || pid.GetOutput() == null)
		{
			alert("Missing PersonalID argument");
			return;
		}

		var URL = "/login?PID=" + pid.GetOutput() + "&Name=" + name.GetOutput();
		if(other != null && other.GetOutput() != null)
		{
			if(type == 1)
				URL += "&DID=" + other.GetOutput();
			else if(type == 2)
				URL += "&UID=" + other.GetOutput();
		}

		var onfinish = function(responseText)
		{
			var res = JSON.parse(responseText);
			if(res.code != 1)
				alert("Registering Failed : code : " + res.code + " : " + res.status);
			window.location.reload(true);
		};
		var onerror = function(statusText)
		{
			alert("Login Error : " + statusText);
		};

		HttpAsyncRequest("POST",URL,null,onfinish,onerror);

		closeTab();
	};
}

function thankyoupage()
{
	document.getElementById("registerform").fading(666,0,100);
	document.getElementById("registerform").rows[0].cells[0].innerHTML = LoadFile("GET","RegisFinish.htm");

	document.getElementById("thankText").setAttribute("class","Header-Bold-text-Type" + RegisterTypeSeleced);
	document.getElementById("thankImg").setAttribute("src","imgs/doctype" + RegisterTypeSeleced + ".png");
	document.getElementById("thankButton").setAttribute("class","bttab" + RegisterTypeSeleced);
}

function ResendEmail()
{
	document.getElementById("registerform").fading(666,0,100);
	document.getElementById("registerform").rows[0].cells[0].innerHTML = LoadFile("GET","ResendForm.htm");

	document.getElementById("resendTypeImg").setAttribute("src","imgs/doctype" + RegisterTypeSeleced + ".png");
	document.getElementById("resendTypeText").setAttribute("class","Header-Bold-text-Type" + RegisterTypeSeleced);
	document.getElementById("resendTypeLink").setAttribute("class","bttab" + RegisterTypeSeleced);
	document.getElementById("resendTypeClose").setAttribute("class","bttab" + RegisterTypeSeleced);

	document.getElementById("Email_RegF").InitPlaceHolder();
}

function SetType(key,i)
{
	var type = document.getElementById("type" + i);
	type.setAttribute("src","imgs/doctype" + i + ".png");
	type.onmouseover = function()
	{
		type.setAttribute("src","imgs/doctype" + i + "-2.png");
	};
	type.onmouseout = function()
	{
		type.setAttribute("src","imgs/doctype" + i + ".png");
	};
	type.onclick = function()
	{
		Select(key,i);
	};
}
function LogIn(responseText)
{
	if(responseText == null || responseText.length < 1)
		return;

	var res = JSON.parse(responseText);
	switch(res.code)
	{
		case 3 :
			document.getElementById("registertab").style.visibility = "visible";
			document.getElementById("registertab").fading(666,0,100);
			ResendEmail();
		break;

		case 2 :
			if(res.key == null || res.key.length < 1)
			{
				alert("Why you can access here with this key ( " + key + " )!?, Don't Hack!");
				return;
			}

			document.getElementById("registerform").rows[0].cells[0].innerHTML = LoadFile("GET","RegisSelect.htm");
			SetType(res.key,1);
			SetType(res.key,2);
			SetType(res.key,3);

			document.getElementById("registertab").style.visibility = "visible";
			document.getElementById("registertab").fading(666,0,100);
		break;

		case 1 :
			window.location.reload(true);
		break;

		default :
			alert("Login Failed : code : " + res.code + " : " + res.status);
		break;
	}
}

window.onload = function()
{
	document.getElementById("RegisterID").InitPlaceHolder();
	document.getElementById("RegisterPass").InitPlaceHolder();
	document.getElementById("RegisterRepeatPass").InitPlaceHolder();

	document.getElementById("LogInEmail").InitPlaceHolder();
	document.getElementById("LogInPass").InitPlaceHolder();

	var onFinish = function(responseText)
	{
		LogIn(responseText);
	};
	var onError = function(statusText)
	{
		alert("Login error loaded : " + statusText);
	};
	HttpAsyncRequest("GET","/login",null,onFinish,onError);

	var logIn = document.getElementById("loginbt");
	logIn.onmouseover = function()
	{
		logIn.goLite();
	};
	logIn.onmouseout = function()
	{
		logIn.goDim();
	};
	logIn.onclick = function()
	{
		var email = document.getElementById("LogInEmail").GetOutput();
		var pass = document.getElementById("LogInPass").GetOutput();
		var cookie = document.getElementById("IsCookie").checked;
		if(email != null && pass != null)
		{
			var onFinish = function(responseText)
			{
				LogIn(responseText);
			};
			var onError = function(statusText)
			{
				alert("Login error : " + statusText);
			};
			HttpAsyncRequest("GET","/login?email=" + email + "&password=" + pass + "&cookie=" + (cookie ? "true" : "false"),null,onFinish,onError);
		}
	};
};
